package com.fanhao.dllo.justforyou.helper;

import com.fanhao.dllo.justforyou.application.BaseApplication;
import com.fanhao.dllo.justforyou.entity.greendao.DaoSession;
import com.fanhao.dllo.justforyou.entity.greendao.PersonEntity;
import com.fanhao.dllo.justforyou.entity.greendao.PersonEntityDao;

import java.util.List;

import de.greenrobot.dao.query.DeleteQuery;
import de.greenrobot.dao.query.QueryBuilder;

/**
 * Created by dllo on 15/10/28.
 */
public class PersonHelper {
    private static PersonHelper helper;
    private static DaoSession daoSession;
    public PersonHelper() {

    }

    /***
     * 通过静态方法直接获取给实例对象
     * ***/

    public  static PersonHelper getInstance() {
        if (helper == null) {
            helper = new PersonHelper();
            daoSession = BaseApplication.getDaoSession();
        }

        return helper;

    }

    /**往数据库中添加集合对象**/
    public void savePersonEntityList(PersonEntity personEntity) {
        daoSession.getPersonEntityDao().insertInTx(personEntity);

    }

    /***查询数据***/
    public List<PersonEntity> queryAll() {
        return daoSession.getPersonEntityDao().loadAll();

    }
    public boolean getName(String name){
        QueryBuilder<PersonEntity> qbBuilder = daoSession.getPersonEntityDao().queryBuilder();
        qbBuilder.where(PersonEntityDao.Properties.Name.eq(name));
        return qbBuilder.buildCount().count()>0 ?
                true:false;
    }


    public void DeleteAll(){
        daoSession.getPersonEntityDao().deleteAll();
    }

    //按name返回某一个对象
    public PersonEntity getPersonEntityByName(String name){
        QueryBuilder<PersonEntity> qb = daoSession.getPersonEntityDao().queryBuilder();
        qb.where(PersonEntityDao.Properties.Name.eq(name));

        if (qb.list() != null && qb.list().size() > 0 ) {
            return qb.list().get(0);
        }
        return null;
    }

}
